﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CE.iPhone.PList;
using cczEdit.DramaDialog;
namespace cczEdit.Data
{
    class InstructMap
    {
        PListDict instructMapRootDict;
        PListDict dataRootDict;
        public InstructMap(PListDict dataRootDict_)
        {
            PListRoot root = PListRoot.Load(Form1.cczDirector + "\\InstructMap.plist");
            instructMapRootDict = (PListDict)root.Root;
            dataRootDict = dataRootDict_;
        }
        public PListDict MappingTestInstruct(int instructPCID,List<String> paramValues)
        {
            PListDict dict ;
            long[] symbolMap = new long[] { 7, 6, 4 };
            if (instructPCID == 5)
            {
                //变量测试
                dict = AddDramaDialog.CreateConditionDict(kLogicalTestType.kLogicalTestTypeVariable);
                PListArray trueVariables = (PListArray)dict["TrueVariables"];
                PListArray falseVariables = (PListArray)dict["FalseVariables"];
                int flaseVarIndex = 1;
                for (int i = 0; i != paramValues.Count; ++i)
                {
                    if (paramValues[i] == "-1")
                    {
                        flaseVarIndex = i + 1;
                        break;
                    }
                    trueVariables.Add(new PListInteger(long.Parse(paramValues[i])));
                }
                for (int i = flaseVarIndex; i < paramValues.Count; ++i)
                {
                    falseVariables.Add(new PListInteger(long.Parse(paramValues[i])));
                }
            }else if(instructPCID == 37)
            {
                //武将进入指定地点测试
                dict = AddDramaDialog.CreateConditionDict(kLogicalTestType.kLogicalTestTypeLocationOfCoord);
                long unitID = long.Parse(paramValues[0]);
                dict["UnitID"] = new PListInteger(CorrectUnitID(unitID));
                dict["CoordX"] = new PListInteger(long.Parse(paramValues[1]));
                dict["CoordY"] = new PListInteger(long.Parse(paramValues[2]));
            }
            else if (instructPCID == 38)
            {
                //武将进入指定区域测试

                dict = AddDramaDialog.CreateConditionDict(kLogicalTestType.kLogicalTestTypeLocationOfRange);
                long unitID = long.Parse(paramValues[0]);
                dict["UnitID"] = new PListInteger(CorrectUnitID(unitID));
                dict["OriginX"] = new PListInteger(long.Parse(paramValues[1]));
                dict["OriginY"] = new PListInteger(long.Parse(paramValues[2]));
                dict["DstX"] = new PListInteger(long.Parse(paramValues[3]));
                dict["DstY"] = new PListInteger(long.Parse(paramValues[4]));
            }
            else if (instructPCID == 46)
            {
                //武将相邻测试

                dict = AddDramaDialog.CreateConditionDict(kLogicalTestType.kLogicalTestTypeNeightbour);
                
                dict["UnitID1"] = new PListInteger(CorrectUnitID( long.Parse(paramValues[0])));
                dict["UnitID2"] = new PListInteger(CorrectUnitID( long.Parse(paramValues[1])));
            }
            else if (instructPCID == 54)
            {
                //武将状态测试

                dict = AddDramaDialog.CreateConditionDict(kLogicalTestType.kLogicalTestTypeState);

                dict["UnitID"] = new PListInteger(CorrectUnitID( long.Parse(paramValues[0])));

                dict["AttributeType"] = new PListInteger(long.Parse(paramValues[1]));
                dict["Value"] = new PListInteger(long.Parse(paramValues[2]));
                long symbol = long.Parse(paramValues[3]);
                dict["SymbolType"] = new PListInteger(symbolMap[symbol]);

            }
            else if (instructPCID == 55)
            {
                //钱剧本野心测试

                dict = AddDramaDialog.CreateConditionDict(kLogicalTestType.kLogicalTestTypeMoneyDramaAmbition);

                dict["PropertyType"] = new PListInteger(long.Parse(paramValues[0]));
                dict["Value"] = new PListInteger(long.Parse(paramValues[1]));
                long symbol = long.Parse(paramValues[2]);
                dict["SymbolType"] = new PListInteger(symbolMap[symbol]);
            }
            else if (instructPCID == 63)
            {
                //回合测试

                dict = AddDramaDialog.CreateConditionDict(kLogicalTestType.kLogicalTestTypeTurn);

                dict["Value"] = new PListInteger(long.Parse(paramValues[0]));
                long symbol = long.Parse(paramValues[1]);
                dict["SymbolType"] = new PListInteger(symbolMap[symbol]);
            }
            else if (instructPCID == 64)
            {
                //行动方测试

                dict = AddDramaDialog.CreateConditionDict(kLogicalTestType.kLogicalTestTypeOwner);

                dict["Owner"] = new PListInteger(long.Parse(paramValues[0]));
            }
            else if (instructPCID == 65)
            {
                //战场人数测试

                dict = AddDramaDialog.CreateConditionDict(kLogicalTestType.kLogicalTestTypeAmount);

                dict["Owner"] = new PListInteger(long.Parse(paramValues[0]));
                dict["Value"] = new PListInteger(long.Parse(paramValues[1]));
                long symbol = long.Parse(paramValues[2]);
                dict["SymbolType"] = new PListInteger(symbolMap[symbol]);
                int rangeType = int.Parse(paramValues[3]);
                if (rangeType == 1)
                {
                    dict["OriginX"] = new PListInteger(long.Parse(paramValues[4]));
                    dict["OriginY"] = new PListInteger(long.Parse(paramValues[5]));
                    dict["DstX"] = new PListInteger(long.Parse(paramValues[6]));
                    dict["DstY"] = new PListInteger(long.Parse(paramValues[7]));

                }
                else
                {
                    dict["OriginX"] = new PListInteger(long.Parse(paramValues[4]));
                    dict["OriginY"] = new PListInteger(long.Parse(paramValues[5]));
                    dict["DstX"] = new PListInteger(-1);
                    dict["DstY"] = new PListInteger(-1);
                }
            }
            else if (instructPCID == 66)
            {
                //战斗胜利测试
                dict = AddDramaDialog.CreateConditionDict(kLogicalTestType.kLogicalTestTypeWin);

            }
            else if (instructPCID == 67)
            {
                //战斗失败测试
                dict = AddDramaDialog.CreateConditionDict(kLogicalTestType.kLogicalTestTypeLose);

            }
            else
            {
                //未知测试-默认变量测试
                dict = AddDramaDialog.CreateConditionDict(kLogicalTestType.kLogicalTestTypeVariable);

            }
            return dict;
        }

        public List<PListDict> MappingCommonInstruct(int instructPCID, List<String> paramValues)
        {
            List<PListDict> instructList = new List<PListDict>();
            PListDict instructCommon = (PListDict)instructMapRootDict["InstructCommon"];
            if (instructCommon.Keys.Contains(instructPCID + ""))
            {
                PListDict dataDict = (PListDict)instructCommon[instructPCID + ""];
                PListInteger instructType = (PListInteger)dataDict["NewInstruct"];
                PListDict newInstruct = AddDramaDialog.CreateInstructDict((kInstructType)instructType.Value);
                PListArray paramList = (PListArray)dataDict["ParamMap"];
                for (int i = 0; i < paramList.Count; ++i)
                {
                    PListString elem = (PListString)paramList[i];
                    if (elem == "-1")
                        continue;
                    IPListElement param = newInstruct[elem.Value];
                    if (param is PListInteger)
                    {
                        newInstruct[elem.Value] = new PListInteger(long.Parse(paramValues[i]));
                    }
                    else if (param is PListString)
                    {
                        newInstruct[elem.Value] = new PListString(paramValues[i]);
                    }
                    else if (param is PListBool)
                    {
                        int parmaValue = int.Parse(paramValues[i]);
                        newInstruct[elem.Value] = new PListBool(parmaValue > 0);
                    }
                }
                instructList.Add(newInstruct);
                //correct some parameter
                CorrectInstruct(instructPCID, newInstruct, paramValues);
            }
            else
            {
                return MappingSpecialInstruct(instructPCID, paramValues);
            }
            
            return instructList;
        }
        private long CorrectUnitID(long unitPCID)
        {
            if (unitPCID == -1) unitPCID = 517;
            if (unitPCID >= 1024) unitPCID -= 511;
            return unitPCID;
        }
        private long CorrectLevel(long levelPC)
        {
            if (levelPC <= 50)
                return levelPC;
            else
                return -1*(levelPC - 50);
        }
        private long CorrectDirection(long directionPCID)
        {
            long[] directionMap = new long[] { 3, 0, 2, 1 };
            if(directionPCID>=255||directionPCID<0)
                directionPCID=0;
            return directionMap[directionPCID];
        }
        private void CorrectInstruct(int instructPCID, PListDict newInstruct, List<String> paramValues)
        {

            if (instructPCID == 80)
            {
                //战场动作修正
                PListInteger sAction = (PListInteger)newInstruct["SActionType"];
                if (sAction.Value < 0 || sAction.Value == 255)
                {
                    sAction = new PListInteger(0);
                }
                newInstruct["SActionType"] = new PListInteger(sAction.Value <= 1 ? sAction.Value + 1 : sAction.Value + 2);
            }
            else if (instructPCID == 6||instructPCID==74)
            {
                //出战限制修正
                PListArray array = (PListArray)newInstruct["UnitsIDLimited"];
                int startIndex = 1;
                if (instructPCID == 6) startIndex = 2;
                else startIndex = 1;

                for (int i = startIndex; i < startIndex+5; ++i)
                {
                    int parmaValue = int.Parse(paramValues[i]);
                    if (parmaValue > 0)
                        array.Add(new PListInteger(parmaValue));
                }
                array = (PListArray)newInstruct["UnitsIDUnBattle"];
                for (int i = startIndex + 5; i < startIndex + 10; ++i)
                {
                    int parmaValue = int.Parse(paramValues[i]);
                    if (parmaValue != -1)
                        array.Add(new PListInteger(parmaValue));
                }
            }
            else if (instructPCID == 52)
            {
                //剧情动作修正
                PListInteger rAction = (PListInteger)newInstruct["RActionType"];
                if (rAction.Value < 0 || rAction.Value == 255)
                {
                    rAction = new PListInteger(0);
                }
                newInstruct["RActionType"] = new PListInteger(rAction.Value == 0 ? rAction.Value + 1 : rAction.Value + 3);
            }
            else if (instructPCID == 17)
            {
                //剧本跳转修正
                PListInteger indexPC = (PListInteger)newInstruct["Index"];
                long realIndex = indexPC.Value / 2;
                bool isSDrama = indexPC.Value % 2 == 0;
                newInstruct["Index"] = new PListInteger(isSDrama?realIndex:realIndex+1);
                newInstruct["IsRDrama"] = new PListBool(!isSDrama);
            }
            else if (instructPCID == 22)
            {
                //信息命令修正
                newInstruct["MessageBoxType"] = new PListInteger((long)kMessageBoxType.kMessageBoxTypeMassWord);
            }
            else if (instructPCID == 25)
            {
                //战役任务命令修正

                String msg = ((PListString)newInstruct["LoseMsg"]).Value;
                String[] lwMsg= msg.Split(new String[]{"\n\n"}, StringSplitOptions.None);
                String winMsg = lwMsg[0].Remove(0, 5);
                String loseMsg = lwMsg[1].Remove(0, 5);
                newInstruct["WinMsg"] = new PListString(winMsg);
                newInstruct["LoseMsg"] = new PListString(loseMsg);
            }
            else if (instructPCID == 32)
            {
                //武将头像状态修正

                long headNum = ((PListInteger)newInstruct["Num"]).Value;
                if (headNum >= 4)
                {
                    newInstruct["Num"] = new PListInteger(0);
                }
            }
            else if (instructPCID == 33)
            {
                //放火命令修正

                newInstruct["ObjectType"] = new PListInteger(0);
                newInstruct["TileType"] = new PListInteger((long)TileType.TileTypeFire);
            }
            else if (instructPCID == 35)
            {
                //音效命令修正

                String nameID = ((PListString)newInstruct["FileName"]).Value;
                String count = paramValues[1];
                String fileName ;
                if (int.Parse(nameID) < 100)
                {
                    fileName = "Se" + nameID + ".wav";
                }
                else if (int.Parse(nameID) >= 100 && int.Parse(nameID) < 200)
                {
                    fileName = "Se_m_" + (int.Parse(nameID) - 100) + ".wav";
                }
                else
                {
                    fileName = "Se_e_" + (int.Parse(nameID) - 200) + ".wav";
                }
                newInstruct["FileName"] = new PListString(fileName);
                if (count == "-1" || count == "255")
                {
                    newInstruct["IsEnd"] = new PListBool(true);
                }
                else if (count == "0")
                {
                    newInstruct["IsLoop"] = new PListBool(true);
                }
                else
                {
                    newInstruct["IsLoop"] = new PListBool(false);
                    newInstruct["IsEnd"] = new PListBool(false);
                }
            }
            else if (instructPCID == 36)
            {
                //背景音乐命令修正

                String nameID = ((PListString)newInstruct["FileName"]).Value;
                if (nameID == "-1"||nameID == "255")
                {
                    newInstruct["IsEnd"] = new PListBool(true);
                }
                else
                {
                    newInstruct["IsEnd"] = new PListBool(false);
                    newInstruct["FileName"] = new PListString("AudioTrack-"+(int.Parse(nameID)+2)+".mp3");
                }
            }
            else if (instructPCID == 44)
            {
                //地图文字命令修正

                newInstruct["MessageBoxType"] = new PListInteger((long)kMessageBoxType.kMessageBoxTypeMapMsg);

            }
            else if (instructPCID == 48 || instructPCID == 51)
            {
                //武将转向，出现命令修正

                PListInteger rAction = (PListInteger)newInstruct["RActionType"];
                PListInteger direction = (PListInteger)newInstruct["Direction"];
                if (rAction.Value < 0 || rAction.Value == 255)
                {
                    rAction = new PListInteger(0);
                }
                newInstruct["RActionType"] = new PListInteger(rAction.Value == 0 ? rAction.Value + 1 : rAction.Value + 3);
                newInstruct["Direction"] = new PListInteger(CorrectDirection(direction.Value));
            }
            else if (instructPCID == 79)
            {
                //战场转向，出现命令修正
                PListInteger direction = (PListInteger)newInstruct["Direction"];
                newInstruct["Direction"] = new PListInteger(CorrectDirection( direction.Value));
            }
            else if (instructPCID == 85)
            {
                //战场复活命令修正
                PListInteger direction = (PListInteger)newInstruct["Direction"];
                newInstruct["Direction"] = new PListInteger(CorrectDirection(direction.Value));
                int dataType = int.Parse(paramValues[0]);
                if (dataType == 0 || dataType == -1)
                {
                    newInstruct["BFIndex"] = new PListInteger(-1);
                }
            }
            else if (instructPCID == 105)
            {
                //旁白命令修正

                newInstruct["MessageBoxType"] = new PListInteger((long)kMessageBoxType.kMessageBoxTypeEquipLvUp);

            }
            else if (instructPCID >= 96 && instructPCID <= 102)
            {
                //单挑相关命令修正
                PListBool isEnemy = (PListBool)newInstruct["IsEnemy"];
                newInstruct["IsEnemy"] =new PListBool(instructPCID!=98?!isEnemy.Value:isEnemy.Value);

                if (instructPCID == 96 || instructPCID == 100)
                {
                    long[] arenaActionMap = new long[] { 1, 13, 14, 15, 16,2, 4, 5, 7, 8, 9, 12, 11, 17 };
                    PListInteger actionTag = (PListInteger)newInstruct["ArenaActionTag"];
                    if (actionTag.Value < 0) actionTag = new PListInteger(0);
                    if (actionTag.Value >= arenaActionMap.Length) actionTag = new PListInteger(6);
                    newInstruct["ArenaActionTag"] = new PListInteger(arenaActionMap[actionTag.Value]);
                }
            }
        }
        private List<PListDict> MappingSpecialInstruct(int instructPCID, List<String> paramValues)
        {
            List<PListDict> instructList = new List<PListDict>();
            long[] computeSymbolMap = new long[] {4,0,1 };
            if (instructPCID == 6 || instructPCID == 74)
            {
                //出战限制修正
                PListDict dict = AddDramaDialog.CreateInstructDict(kInstructType.kInstructTypeLimitPlayerBattle);
                dict["MaxNum"] = new PListInteger(int.Parse(paramValues[1]));

                PListArray array = new PListArray();
                dict["UnitsIDLimited"] = array;
                int startIndex = 1;
                if (instructPCID == 6) startIndex = 2;
                else startIndex = 1;

                for (int i = startIndex; i < startIndex + 5; ++i)
                {
                    int parmaValue = int.Parse(paramValues[i]);
                    if (parmaValue > 0)
                        array.Add(new PListInteger(parmaValue));
                }

                array = new PListArray();
                dict["UnitsIDUnBattle"] = array;
                for (int i = startIndex + 5; i < startIndex + 10; ++i)
                {
                    int parmaValue = int.Parse(paramValues[i]);
                    if (parmaValue != -1)
                        array.Add(new PListInteger(parmaValue));
                }
                instructList.Add(dict);
                dict = AddDramaDialog.CreateInstructDict(kInstructType.kInstructTypeDisplayUI);
                instructList.Add(dict);
            }
            else if (instructPCID == 13 && !FileData.isRDrama)
            {
                //战斗场景结束命令处理
                PListDict dict = AddDramaDialog.CreateInstructDict(kInstructType.kInstructTypeEndScene);
                instructList.Add(dict);

            }
            else if (instructPCID == 20)
            {
                //对话1命令处理

                String param = paramValues[0];
                String[] words = param.Split(new String[]{"&"}, StringSplitOptions.RemoveEmptyEntries);
                foreach (String word in words)
                {
                    string name="";
                    int i = 0;
                    while (word[i] != '\n')
                    {
                        name += word[i];
                        ++i;
                    }
                    Hero hero= UCDrama.heros.Find((h) => { return h.name == name; });
                    PListDict dict = AddDramaDialog.CreateInstructDict(kInstructType.kInstructTypeDialog);
                    dict["Message"] = new PListString(word.Remove(0, i+1));
                    dict["UnitID"] = new PListInteger(hero.num);
                    instructList.Add(dict);
                }
            }
            else if (instructPCID == 39)
            {
                //背景更改命令处理

                PListDict dict = AddDramaDialog.CreateInstructDict(kInstructType.kInstructTypeChangeBg);
                int bgType =int.Parse( paramValues[0]);
                if (bgType == 0)
                {
                    dict["FileName"] = new PListString("Mmap" + paramValues[1]);
                }
                else if (bgType == 1)
                {
                    dict["FileName"] = new PListString("Mmap115");
                }
                else if (bgType == 2)
                {
                    dict["FileName"] = new PListString("Mmap" + (int.Parse( paramValues[3])+41));
                }
                else if (bgType == 3)
                {
                    return instructList;
                }
                instructList.Add(dict);
            }
            else if (instructPCID == 49)
            {
                //武将消失命令处理
                if (FileData.isRDrama)
                {
                    PListDict dict = AddDramaDialog.CreateInstructDict(kInstructType.kInstructTypeObjectHide);
                    dict["UnitID"] = new PListInteger(CorrectUnitID(long.Parse( paramValues[1])));
                    instructList.Add(dict);
                }
                else
                {
                    PListDict dict = AddDramaDialog.CreateInstructDict(kInstructType.kInstructTypeBattleHideUnit);
                    dict["DataType"] = new PListInteger(long.Parse(paramValues[0])+1);
                    dict["Owner"] = new PListInteger(long.Parse(paramValues[6]));
                    PListDict dataDict = (PListDict)dict["Data"];
                    dataDict["OriginX"] = new PListInteger(long.Parse(paramValues[2]));
                    dataDict["OriginY"] = new PListInteger(long.Parse(paramValues[3]));
                    dataDict["DstX"] = new PListInteger(long.Parse(paramValues[4]));
                    dataDict["DstY"] = new PListInteger(long.Parse(paramValues[5]));
                    dataDict["UnitID"] = new PListInteger(CorrectUnitID(long.Parse(paramValues[1])));
                    instructList.Add(dict);
                }
            }
            else if (instructPCID == 50)
            {
                //武将移动命令处理

                if (FileData.isRDrama)
                {
                    PListDict dict = AddDramaDialog.CreateInstructDict(kInstructType.kInstructTypeObjectMove);
                    dict["UnitID"] = new PListInteger(CorrectUnitID(long.Parse(paramValues[1])));
                    dict["DstCoordX"] = new PListInteger(long.Parse(paramValues[3]));
                    dict["DstCoordY"] = new PListInteger(long.Parse(paramValues[4]));
                    dict["Direction"] = new PListInteger(CorrectDirection( long.Parse(paramValues[5])));
                    instructList.Add(dict);
                }
                else
                {
                    PListDict dict = AddDramaDialog.CreateInstructDict(kInstructType.kInstructTypeBattleMove);
                    long dataType = long.Parse(paramValues[0]);
                    dict["BFIndex"] = new PListInteger(dataType==0?-1:long.Parse(paramValues[2]));
                    dict["CoordX"] = new PListInteger(long.Parse(paramValues[3]));
                    dict["CoordY"] = new PListInteger(long.Parse(paramValues[4]));
                    dict["Direction"] = new PListInteger(CorrectDirection( long.Parse(paramValues[5])));
                    dict["UnitID"] = new PListInteger(CorrectUnitID(long.Parse(paramValues[1])));
                    instructList.Add(dict);

                }
            }
            else if (instructPCID == 56)
            {
                //武将能力设定命令处理

                PListDict dict = AddDramaDialog.CreateInstructDict(kInstructType.kInstructTypeSetHeroAbility);
                instructList.Add(dict);
                dict["UnitID"] = new PListInteger(CorrectUnitID(long.Parse(paramValues[0])));
                dict["AttributeType"] = new PListInteger(long.Parse(paramValues[1]));
                dict["Value"] = new PListInteger(long.Parse(paramValues[3]));
                long symbol = long.Parse(paramValues[2]);
                dict["SymbolType"] = new PListInteger(computeSymbolMap[symbol]);
            }
            else if (instructPCID == 58)
            {
                //钱剧本野心设定命令处理
                PListDict dict = AddDramaDialog.CreateInstructDict(kInstructType.kInstructTypeSetMoneyDramaAmbition);
                
                long symbol = long.Parse(paramValues[1]);
                dict["PropertyType"] = new PListInteger(long.Parse(paramValues[0]));
                dict["SymbolType"] = new PListInteger(computeSymbolMap[symbol]);
                dict["Value"] = new PListInteger(long.Parse(paramValues[2]));
                instructList.Add(dict);
            }
            else if (instructPCID == 59)
            {
                //武将加入命令处理

                PListDict dict = AddDramaDialog.CreateInstructDict(kInstructType.kInstructTypeAddHero);
                instructList.Add(dict);
                dict["UnitID"] = new PListInteger(CorrectUnitID(long.Parse(paramValues[0])));
                dict["IsExit"] = new PListBool(long.Parse(paramValues[1])==0?false:true);
                dict["LevelUp"] = new PListInteger(CorrectLevel( long.Parse(paramValues[2])));
            }
            else if (instructPCID == 61)
            {
                //获得物品命令处理

                PListDict dict = AddDramaDialog.CreateInstructDict(kInstructType.kInstructTypeGetItem);
                instructList.Add(dict);
                dict["UnitID"] = new PListInteger(CorrectUnitID(long.Parse(paramValues[3])));
                int sumID = int.Parse(paramValues[0]);
                if (sumID <= 37)
                {
                    dict["ItemType"] = new PListInteger(1);
                    dict["ItemID"] = new PListInteger(sumID);
                }
                else if (sumID >= 38 && sumID <= 52)
                {
                    dict["ItemType"] = new PListInteger(2);
                    dict["ItemID"] = new PListInteger(sumID-38);

                }
                else if (sumID >= 53 && sumID <= 86)
                {
                    dict["ItemType"] = new PListInteger(3);
                    dict["ItemID"] = new PListInteger(sumID - 53);

                }
                else
                {
                    dict["ItemType"] = new PListInteger(0);
                    dict["ItemID"] = new PListInteger(sumID - 87);
                }

            }
            else if (instructPCID == 62 || instructPCID == 72)
            {
                //装备设定命令处理

                PListDict dict = AddDramaDialog.CreateInstructDict(kInstructType.kInstructTypeSetEquipment);
                instructList.Add(dict);
                PListDict dataDicts = (PListDict)dict["DataDic"];
                PListDict dataDict = new PListDict();
                dataDicts.Add(paramValues[0], dataDict);
                for (int i = 1; i <= 5; ++i)
                {
                    if(paramValues[i]=="-1") paramValues[i]="0";
                }
                dataDict["WeaponID"] = new PListInteger(long.Parse(paramValues[1]) - 2);
                dataDict["WeaponLv"] = new PListInteger(long.Parse(paramValues[2]));
                dataDict["ArmorID"] = new PListInteger(long.Parse(paramValues[3])-2);
                dataDict["ArmorLv"] = new PListInteger(long.Parse(paramValues[4]));
                dataDict["AssistID"] = new PListInteger(long.Parse(paramValues[5])-2);
            }
            else if (instructPCID == 76)
            {
                //隐藏武将显示命令处理

                PListDict dict = AddDramaDialog.CreateInstructDict(kInstructType.kInstructTypeBattleDisplayUnit);
                instructList.Add(dict);
                long dataType = long.Parse(paramValues[0]);
                if (dataType == 0)
                {
                    dict["UnitID"] = new PListInteger(CorrectUnitID(long.Parse(paramValues[1])));
                    dict["BFIndex"] = new PListInteger(-1);
                }
                else if (dataType == 1)
                {
                    dict["UnitID"] = new PListInteger(CorrectUnitID(long.Parse(paramValues[1])));
                    dict["BFIndex"] = new PListInteger(long.Parse(paramValues[2]));
                }
            }
            else if (instructPCID == 77)
            {
                //武将状态设定命令处理

                instructList.Add(CreateSetStateInstruct(paramValues));

            }
            else if (instructPCID == 78)
            {
                //武将方针设定命令处理

                instructList.Add(CreateChangeAIInstruct(paramValues));

            }
            else if (instructPCID == 83)
            {
                //武将撤退命令处理

                instructList.Add(CreateBattleExitInstruct(paramValues));

            }
            else if (instructPCID == 88)
            {
                //战场障碍设定命令处理

                instructList.Add(CreateBattleAddObjectInstruct(paramValues));
            }
            else if (instructPCID == 89)
            {
                //战利品命令处理

                instructList.Add(CreateWinRewardInstruct(paramValues));
            }
            else if (instructPCID == 93)
            {
                //最大回合数设定命令处理

                instructList.Add(CreateSetMaxTurnsInstruct(paramValues));
            }
            else if (instructPCID == 69)
            {
                //全局变量设定
                SetGlobalVarInstruct(paramValues);
            }
            else if (instructPCID == 70)
            {
                SetFriendArmyInsrtuct(paramValues);
            }
            else if (instructPCID == 71)
            {
                SetEnemyArmyInsrtuct(paramValues);
            }
            else if (instructPCID == 75)
            {
                PListDict dict= SetPlayerArmyInsrtuct(paramValues);
                if(dict!=null)
                    instructList.Add(dict);

            }
            return instructList;
        }
        private PListDict CreateSetStateInstruct(List<String> paramValues){
            PListDict dict = AddDramaDialog.CreateInstructDict(kInstructType.kInstructTypeBattleSetState);
            long dataType = long.Parse(paramValues[0]);
            if (dataType == 0)
                dataType = 1;
            else if (dataType == 1)
                dataType = 2;
            dict["DataType"] = new PListInteger(dataType);
            dict["Owner"] = new PListInteger(long.Parse(paramValues[7]));
            
            dict["HPDelta"] = new PListInteger(long.Parse(paramValues[11])==-1?0:-1 * long.Parse(paramValues[11]));
            dict["MPDelta"] = new PListInteger(long.Parse(paramValues[12]) == -1 ? 0 : -1 * long.Parse(paramValues[12]));
            PListDict dataDict = (PListDict)dict["Data"];
            dataDict["OriginX"] = new PListInteger(long.Parse(paramValues[3]));
            dataDict["OriginY"] = new PListInteger(long.Parse(paramValues[4]));
            dataDict["DstX"] = new PListInteger(long.Parse(paramValues[5]));
            dataDict["DstY"] = new PListInteger(long.Parse(paramValues[6]));
            dataDict["UnitID"] = new PListInteger(long.Parse(paramValues[1]) == -1 ? 0 : long.Parse(paramValues[1]));
            dataDict["BFIndex"] = new PListInteger(long.Parse(paramValues[2]));
            PListArray states = new PListArray();
            dict["States"] = states;
            long buffType = long.Parse(paramValues[8]);
            if (buffType == 2) buffType = 0;
            if (buffType == 3 || buffType == 4) --buffType;
            long buffChangeType = long.Parse(paramValues[9]);
            if (buffChangeType == 0) buffType += 6;
            else if (buffChangeType == 1) buffType = (long)CharStateChange.CharStateChangeRecover;
            if (buffType != -1)
                states.Add(new PListInteger(buffType));
            long stateType = long.Parse(paramValues[10]);
            if (stateType >= 130)
                states.Add(new PListInteger((long)CharStateChange.CharStateChangeRecover));
            else if (stateType == -1)
            { }
            else
            {
                long[][] stateMap = new long[][] { new long[] {16},new long[] {17}
                         ,new long[] {16,17},new long[] {15},new long[] {15,16},
                         new long[]{15,17},new long[] {15,16,17},new long[] {14},new long[] {14,16},
                         new long[] {14,17},new long[] {14,16,17},new long[] {14,15},
                         new long[] {14,15,16},new long[] {14,15,17},new long[] {14,15,16,17}};
                long[] stateCodes = stateMap[stateType / 2 - 1];
                foreach (long stateCode in stateCodes)
                {
                    states.Add(new PListInteger(stateCode));

                }
            }
            return dict;
        }
        private PListDict CreateChangeAIInstruct(List<String> paramValues)
        {
            PListDict dict = AddDramaDialog.CreateInstructDict(kInstructType.kInstructTypeChangeAI);
            dict["IsZone"] = new PListInteger(long.Parse(paramValues[0]) < 0 ? 0 : long.Parse(paramValues[0]));
            dict["Type"] = new PListInteger(long.Parse(paramValues[7]));

            PListDict dataDict = (PListDict)dict["UnitDic"];
            dataDict["OriginX"] = new PListInteger(long.Parse(paramValues[2]));
            dataDict["OriginY"] = new PListInteger(long.Parse(paramValues[3]));
            dataDict["DstX"] = new PListInteger(long.Parse(paramValues[4]));
            dataDict["DstY"] = new PListInteger(long.Parse(paramValues[5]));
            dataDict["UnitID"] = new PListInteger(CorrectUnitID(long.Parse(paramValues[1])));
            dataDict["Owner"] = new PListInteger(long.Parse(paramValues[6]));
            PListDict targetDict = (PListDict)dict["TargetDic"];
            targetDict["CoordX"] = new PListInteger(long.Parse(paramValues[9]));
            targetDict["CoordY"] = new PListInteger(long.Parse(paramValues[10]));
            targetDict["UnitID"] = new PListInteger(CorrectUnitID(long.Parse(paramValues[8])));

            return dict;
        }
        private PListDict CreateBattleExitInstruct(List<String> paramValues)
        {
            PListDict dict = AddDramaDialog.CreateInstructDict(kInstructType.kInstructTypeBattleExit);
            dict["DataType"] = new PListInteger(long.Parse(paramValues[0])+1);
            dict["Owner"] = new PListInteger(long.Parse(paramValues[6]) < 0 ? 0 : long.Parse(paramValues[6]));
            dict["IsDead"] = new PListBool(long.Parse(paramValues[7])>0);

            PListDict dataDict = (PListDict)dict["Data"];
            dataDict["OriginX"] = new PListInteger(long.Parse(paramValues[2]));
            dataDict["OriginY"] = new PListInteger(long.Parse(paramValues[3]));
            dataDict["DstX"] = new PListInteger(long.Parse(paramValues[4]));
            dataDict["DstY"] = new PListInteger(long.Parse(paramValues[5]));
            dataDict["UnitID"] = new PListInteger(CorrectUnitID(long.Parse(paramValues[1])));
            return dict;
        }
        private PListDict CreateBattleAddObjectInstruct(List<String> paramValues)
        {
            PListDict dict = AddDramaDialog.CreateInstructDict(kInstructType.kInstructTypeBattleAddObject);
            dict["ObjectType"] = new PListInteger(long.Parse(paramValues[0]) >= 3 ? 3 : long.Parse(paramValues[0]));
            dict["TileType"] = new PListInteger(long.Parse(paramValues[2]));
            dict["CoordX"] = new PListInteger(long.Parse(paramValues[3]));
            dict["CoordY"] = new PListInteger(long.Parse(paramValues[4]));
            if (long.Parse(paramValues[0]) >= 3)
            {
                int indexPC= int.Parse(paramValues[0])-4;
                dict["GateIndex"] = new PListInteger(indexPC * 2 + long.Parse(paramValues[1]));
            }

            return dict;
        }
        private PListDict CreateWinRewardInstruct(List<String> paramValues)
        {
            PListDict dict = AddDramaDialog.CreateInstructDict(kInstructType.kInstructTypeBattleDisplayWin);
            dict["Money"] = new PListInteger(long.Parse(paramValues[0]));
            PListDict itemDic = (PListDict)dict["ItemDic"];

            for (int i = 1; i <= 5; i += 2)
            {
                PListDict item = new PListDict();
                long sumID = long.Parse(paramValues[i]);
                if (sumID == -1) continue;
                long itemLv = long.Parse(paramValues[i+1]);
                Sundry equip;
                if (sumID <= 37)
                {
                    item["Type"] = new PListString("Weapon");
                    item["Num"] = new PListInteger(sumID);
                    equip= UCDrama.weapons.Find((w) => { return w.num == sumID; });
                    
                }
                else if (sumID >= 38 && sumID <= 52)
                {
                    item["Type"] = new PListString("Armor");
                    item["Num"] = new PListInteger(sumID - 38);
                    equip = UCDrama.armors.Find((w) => { return w.num == sumID-38; });

                }
                else if (sumID >= 53 && sumID <= 86)
                {
                    item["Type"] = new PListString("Assist");
                    item["Num"] = new PListInteger(sumID - 53);
                     equip = UCDrama.assists.Find((w) => { return w.num == sumID-53; });

                }
                else
                {
                    item["Type"] = new PListString("Sundry");
                    item["Num"] = new PListInteger(sumID - 87);
                     equip = UCDrama.sundries.Find((w) => { return w.num == sumID-87; });

                }
                item["Level"] = new PListInteger(itemLv<0?0:itemLv);
                itemDic.Add(equip.name, item);

            }

            return dict;
        }
        private PListDict CreateSetMaxTurnsInstruct(List<String> paramValues)
        {
            long[] computeSymbolMap = new long[] { 4, 0, 1 };

            PListDict dict = AddDramaDialog.CreateInstructDict(kInstructType.kInstructTypeSetMaxTurns);
            dict["SymbolType"] = new PListInteger(computeSymbolMap[long.Parse(paramValues[0])]);
            dict["MaxTurns"] = new PListInteger(long.Parse(paramValues[1]));

            return dict;
        }
        private void SetGlobalVarInstruct(List<String> paramValues)
        {
            PListArray preSettingArray = (PListArray)dataRootDict["PreSetting"];
            PListDict globalVarDict=new PListDict();
            PListDict placeNameDict = new PListDict();
            foreach (PListDict dict in preSettingArray)
            {
                if (((PListInteger)dict["InstructType"]) == 46)
                {
                    globalVarDict = dict;
                }
                if (((PListInteger)dict["InstructType"]) == 6)
                {
                    placeNameDict = dict;
                }
            }

            globalVarDict["MaxTurns"] = new PListInteger(long.Parse(paramValues[2]));
            long level = long.Parse(paramValues[3]);
            globalVarDict["Level"] = new PListInteger(CorrectLevel(level));
            globalVarDict["Weather"] = new PListInteger(long.Parse(paramValues[8]));
            string[] temp = FileData.fileName.Split(new String[]{"\\"},StringSplitOptions.RemoveEmptyEntries);
            string fileName = temp[temp.Length - 1];
            long mapIndex=0;
            if (fileName[2] == '0')
            {
                mapIndex = long.Parse("" + fileName[3]);
                globalVarDict["MapIndex"] = new PListInteger(mapIndex);

            }
            else
            {
                mapIndex = long.Parse(fileName[2] + "" + fileName[3]);
            }
            globalVarDict["MapIndex"] = new PListInteger(mapIndex);
            PListArray missonNames = (PListArray)instructMapRootDict["MissionName"];
            placeNameDict["PlaceName"] = new PListString(((PListString)missonNames[(int)mapIndex]).Value);
        }
        private void SetFriendArmyInsrtuct(List<String> paramValues)
        {
            PListArray preSettingArray = (PListArray)dataRootDict["PreSetting"];
            PListDict friendsDict = new PListDict();
            foreach (PListDict dict in preSettingArray)
            {
                if (((PListInteger)dict["InstructType"]) == 47)
                {
                    friendsDict = dict;
                    break;
                }
            }
            PListDict datas = (PListDict)friendsDict["DataDic"];
            for (int i = 0; i < paramValues.Count; i += 11)
            {
                if (long.Parse(paramValues[i]) == -1 || datas.Keys.Contains("Unit" + long.Parse(paramValues[i])))
                    continue;
                PListDict dataDict = new PListDict();
                datas.Add("Unit"+long.Parse(paramValues[i]),dataDict);
                dataDict["UnitNum"] = new PListInteger(long.Parse( paramValues[i]));
                dataDict["IsHidden"] = new PListBool(long.Parse(paramValues[i+1])>=1);
                dataDict["CoordX"] = new PListInteger(long.Parse(paramValues[i + 2]));
                dataDict["CoordY"] = new PListInteger(long.Parse(paramValues[i + 3]));
                if (paramValues[i + 4] == "-1") paramValues[i + 4] = "0";
                dataDict["Direction"] = new PListInteger(CorrectDirection( long.Parse(paramValues[i + 4])));
                dataDict["LevelAdd"] = new PListInteger(CorrectLevel(long.Parse(paramValues[i + 5])));
                long teamLimit = long.Parse(paramValues[i + 6]);
                if(teamLimit==-1) teamLimit=2;
                dataDict["TeamLimit"] = new PListInteger(teamLimit);
                dataDict["IsEnemyHelper"] = new PListBool(false);
                
                PListDict aiDataDict = new PListDict();
                dataDict.Add("AIType",aiDataDict);
                aiDataDict["Type"] = new PListInteger(long.Parse(paramValues[i + 7]));
                PListDict targetDict = new PListDict();
                aiDataDict.Add("Target", targetDict);
                targetDict["CoordX"] = new PListInteger(long.Parse(paramValues[i + 9]));
                targetDict["CoordY"] = new PListInteger(long.Parse(paramValues[i + 10]));
                targetDict["UnitID"] = new PListInteger(CorrectUnitID(long.Parse(paramValues[i + 8])));
            }
           
        }
        private void SetEnemyArmyInsrtuct(List<String> paramValues)
        {
            PListArray preSettingArray = (PListArray)dataRootDict["PreSetting"];
            PListDict enemysDict = new PListDict();
            foreach (PListDict dict in preSettingArray)
            {
                if (((PListInteger)dict["InstructType"]) == 48)
                {
                    enemysDict = dict;
                    break;
                }
            }
            PListDict datas = (PListDict)enemysDict["DataDic"];
            for (int i = 0; i < paramValues.Count; i += 12)
            {
                if (long.Parse(paramValues[i]) == -1)
                    continue;
                PListDict dataDict = new PListDict();
                datas.Add("Unit" + long.Parse(paramValues[i]), dataDict);
                dataDict["UnitNum"] = new PListInteger(long.Parse(paramValues[i]));
                dataDict["IsEnemyHelper"] = new PListBool(long.Parse(paramValues[i + 1])>=1);
                dataDict["IsHidden"] = new PListBool(long.Parse(paramValues[i + 2]) >= 1);
                dataDict["CoordX"] = new PListInteger(long.Parse(paramValues[i + 3]));
                dataDict["CoordY"] = new PListInteger(long.Parse(paramValues[i + 4]));
                if (paramValues[i + 5] == "-1") paramValues[i + 5] = "0";
                dataDict["Direction"] = new PListInteger(CorrectDirection( long.Parse(paramValues[i + 5])));
                dataDict["LevelAdd"] = new PListInteger(CorrectLevel(long.Parse(paramValues[i + 6])));
                long teamLimit = long.Parse(paramValues[i + 7]);
                if (teamLimit == -1) teamLimit = 2;
                dataDict["TeamLimit"] = new PListInteger(teamLimit);


                PListDict aiDataDict = new PListDict();
                dataDict.Add("AIType", aiDataDict);
                aiDataDict["Type"] = new PListInteger(long.Parse(paramValues[i + 8]));
                PListDict targetDict = new PListDict();
                aiDataDict.Add("Target", targetDict);
                targetDict["CoordX"] = new PListInteger(long.Parse(paramValues[i + 10]));
                targetDict["CoordY"] = new PListInteger(long.Parse(paramValues[i + 11]));
                targetDict["UnitID"] = new PListInteger(CorrectUnitID(long.Parse(paramValues[i + 9])));
            }

        }
        private PListDict SetPlayerArmyInsrtuct(List<String> paramValues)
        {
            PListArray preSettingArray = (PListArray)dataRootDict["PreSetting"];
            PListDict playerBattleDict = new PListDict();
            foreach (PListDict dict in preSettingArray)
            {
                if (((PListInteger)dict["InstructType"]) == 49)
                {
                    playerBattleDict = dict;
                    break;
                }
            }
            
            PListDict dataDic = (PListDict)playerBattleDict["DataDic"];
            bool isInPreSetting=true;
            if (dataDic.Keys.Contains("BF" + paramValues[0]))
            {
                isInPreSetting = false;
                playerBattleDict = AddDramaDialog.CreateInstructDict(kInstructType.kInstructTypeSetPlayerBattle);
                dataDic = (PListDict)playerBattleDict["DataDic"];
            }
            else
            {
                isInPreSetting = true;
            }
            PListDict bfData = new PListDict();
            dataDic.Add("BF" + paramValues[0], bfData);

            bfData["BFIndex"] = new PListInteger(long.Parse(paramValues[0]));
            bfData["CoordX"] = new PListInteger(long.Parse(paramValues[1]));
            bfData["CoordY"] = new PListInteger(long.Parse(paramValues[2]));
            bfData["Direction"] = new PListInteger(CorrectDirection( long.Parse(paramValues[3])));
            bfData["IsHidden"] = new PListBool(long.Parse(paramValues[4]) > 0);
            bfData["IsEnemyHelper"] = new PListBool(false);
            if (!isInPreSetting)
                return playerBattleDict;
            else
                return null;
        }
    }
}
